package com.wang.myapplication.base;

/**
 * Created by xhwang on 2018/3/14.
 * 看着简单但是貌似有弊端  性能不太好
 */

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import butterknife.ButterKnife;


public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * 当前Activity渲染的视图View
     **/
    public View mContentView = null;
    /**
     * 是否输出日志信息
     **/
    private boolean isDebug = true;
    private String APP_NAME = "Wangxq";
    protected final String TAG = this.getClass().getSimpleName();
    /**
     * 当前Activity
     */
    public Activity mCurActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCurActivity = this;
        Bundle bundle = getIntent().getExtras();
        initParams(bundle);
        setBaseView(bindLayout());
        initView(savedInstanceState, mContentView);
        doBusiness(this);
        ButterKnife.bind(this);
    }


    protected void setBaseView(@LayoutRes int layoutId) {
        if (layoutId <= 0) return;
        setContentView(mContentView = LayoutInflater.from(this).inflate(layoutId, null));
    }






    /**
     * [初始化Bundle参数]
     *
     * @param params
     */
    public abstract void initParams(Bundle params);

    /**
     * [绑定布局]
     *
     * @return
     */
    public abstract int bindLayout();


    /**
     * [初始化控件]
     *
     * @param view
     */
    public abstract void initView(final Bundle savedInstanceState,final View view);

    /**
     * [业务操作]
     *
     * @param mContext
     */
    public abstract void doBusiness(Context mContext);

    /**
     * View点击
     **/
    public abstract void widgetClick(View v);





    @Override
    public void onClick(View v) {
        if (!isFastClick())
            widgetClick(v);
    }

    /**
     * [页面跳转]
     *
     * @param clz
     */
    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }

    /**
     * [携带数据的页面跳转]
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T $(int resId) {
        return (T) super.findViewById(resId);
    }

    /**
     * [含有Bundle通过Class打开编辑界面]
     *
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * [日志输出]
     *
     * @param msg
     */
    protected void $Log(String msg) {
        if (isDebug) {
            Log.d(APP_NAME, msg);
        }
    }

    /**
     * [防止快速点击]
     *
     * @return
     */
    long lastClick = 0;

    private boolean isFastClick() {
        boolean isFastClick;

        if (System.currentTimeMillis() - lastClick <= 1000)
            isFastClick = true;
        else
            isFastClick = false;

        lastClick = System.currentTimeMillis();
        return isFastClick;
    }

    public void Toast(String text) {
        Toast.makeText(mCurActivity, "" + text, Toast.LENGTH_SHORT).show();
    }

    public void Toast(int resId) {
        Toast.makeText(mCurActivity, "" + String.valueOf(resId), Toast.LENGTH_SHORT).show();
    }

}